package cn.circle.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.circle.commom.controller.BackendBaseController;
import cn.circle.system.service.ICommodityCategoryService;
import cn.circle.system.model.CommodityCategory;
import cn.circle.system.pagemodel.DataGrid;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.pagemodel.ReturnInfo;

/**
 * 商品类别控制器类
 * 
 * @author 
 */
@Controller
@RequestMapping("/manage/commodityCategory")
public class CommodityCategoryController extends BackendBaseController {

	private static final Log log = LogFactory.getLog(CommodityCategoryController.class);

	@Autowired
	private ICommodityCategoryService commodityCategoryService;

	/**
	 * 
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {

		return super.index("/WEB-INF/pages/system/commodity_category/commodity_category_list.jsp", request);
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute CommodityCategory c, PageHelper ph) {
		ph.setSort("categoryId");
		ph.setOrder("desc");
		DataGrid dg = commodityCategoryService.datagrid(ph,c);
		return dg;
	}

	/**
	 * 跳转至 sysmenus_au页(添加和修改共用一个编辑页)
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		map.put("saveUrl", "/manage/commodityCategory/addSave");
		return "/WEB-INF/pages/system/commodity_category/commodity_category_au.jsp";
	}

	/**
	 * 保存添加数据
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid CommodityCategory c, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (c != null) {
				try {
					commodityCategoryService.save(c);
				} catch (Exception e) {
					log.info(""+e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}

		return result;
	}

	/**
	 * 跳转至 sysmenus_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		map.put("saveUrl", "/manage/commodityCategory/editSave");
		if (id != null) {
			List<CommodityCategory> model = commodityCategoryService.getOne(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/commodity_category/commodity_category_au.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid CommodityCategory c, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			log.info("数据校验出错！");
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (c != null) {
				try {
					commodityCategoryService.update(c);
				} catch (Exception e) {
					log.info("修改异常" + e.getMessage());
					result.setMsg("修改失败");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 物理删除,从数据库中删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String ids, ReturnInfo result) {
		if (ids != null) {
			try {
				commodityCategoryService.deleteAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

	   return result;
	}

	/**
	 * 移除,逻辑删除，将状态修改为0
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String ids, ReturnInfo result) {

		if (ids != null) {
			try {
				commodityCategoryService.removeAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}

	/**
	 * 获得商品类别
	 */
	@ResponseBody
	@RequestMapping(value = "/getCategory")
	public DataGrid getCategory(DataGrid result) {

		try {
			List<CommodityCategory> lst = commodityCategoryService.getCategory();
			result.setRows(lst);
		} catch (Exception e) {
			log.info("异常" + e.getMessage());
		}

		return result;
	}
}
